//
// Created by summerday on 2021/6/3.
//

#include <cstdio>
#include<iostream>

using namespace std;
typedef struct DNode {
    int data; // 数据域
    struct DNode *next; // 指针域
    struct DNode *prev;
} DNode, *DLinkList;



// 初始化双链表
bool initDLinkList(DLinkList &list) {
    list = (DNode *) malloc(sizeof(DNode *));
    if (list == nullptr) return false;
    list->prev = nullptr;
    list->next = nullptr;
    return true;
}

bool empty(DLinkList list){
    return list->next == nullptr;
}


// p后插s
bool insertNextNode(DNode *p, DNode *s) {
    if(!p || !s) return false;
    s->next = p->next;
    if(p->next != nullptr)
        p->next->prev = s;
    s->prev = p;
    p->next = s;
    return true;
}

// 删除p的后继
bool deleteNextNode(DNode *p){
    if(p == NULL) return false;
    DNode  *q = p->next;
    if(q == NULL) return false; // P 无后继
    p->next = q->next;
    if(q->next != NULL) q->next->prev = p; // q不是最后一个结点
    free(q);
    return true;
}

void destroyList(DLinkList &list){
    while(list->next != NULL){
        deleteNextNode(list);
    }
    free(list);
    list = NULL;
}

int len(DLinkList list) {
    int s = 0;
    for (DNode *p = list->next; p; p = p->next, s++);
    return s;
}

void print(DLinkList list) {
    for (DNode *p = list->next; p; p = p->next) {
        cout << p->data << " ";
    }
    cout << endl;
}

void infoShow(DLinkList list) {
    cout << "------ info show ------" << endl;
    cout << "length : " << len(list) << endl;
    print(list);
}

